package org.sao.auth.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.sao.auth.feign.UserService;
import org.sao.auth.util.UserDetailsImpl;
import org.sao.common.vo.SysRole;
import org.sao.common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;




/**
 * @author lenovo
 * 
 */
@Service("userDetailService")
public class UserDetailServiceImpl   implements  UserDetailsService,Serializable{
	
	@Autowired
	private UserService userService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		UserVo userVo= userService.findUserByUsername(username);
		if(userVo==null){
			 throw new UsernameNotFoundException("UserDetailServiceImpl:用户不存在");
		}
		 List<GrantedAuthority> authorityList = new ArrayList<>();
	        for (SysRole role : userVo.getRoleList()) {
	            authorityList.add(new SimpleGrantedAuthority(role.getRoleCode()));
	        }
		return new  UserDetailsImpl(userVo);
	}
	
	

}
